/******************************************************
Cours:   LOG121
Session: A2013
Groupe:  04
Projet: Laboratoire #4
Étudiant(e)s: Fredy Alexander Bonilla, Renaud Vincent, Simon Castro
                            
Professeur : Francis Cardinal
Nom du fichier: PanelVignetteOptions.java
Date cree: 2013-12-01
Date dern. modif. 2013-12-01
*******************************************************
Historique des modifications
*******************************************************
*******************************************************/  

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;

/**
 * La classe PanelVignetteOptions herite de JPanel et implemente Observer. Elle 
 * contient les methode pour itialiser les buttons d'action pour tracer la
 * vignette de l'image.
 * 
 * @author Simon
 *
 */
public class PanelVignetteOptions extends JPanel implements Observer {
	
	private static final long serialVersionUID = 1L;
	
	//Constante de dimension pour les bouttons
	private static final Dimension DIM_BOUTTONS = new Dimension(50,50);
	
	//Attributs
	private Modele modele;
	private Controleur controleur;
	private Image img;
	private Dimension taille;
	
	private ImageIcon undoIcon;
	private ImageIcon redoIcon;
	private ImageIcon zoomInIcon;
	private ImageIcon zoomOutIcon;
	
	private JButton zoomIn;
	private JButton zoomOut;
	private JButton undo;
	private JButton redo;
	
	private ButtonGroup groupe;
	
	/**
	 * Constructeur de le classe, avec l'image et les bouttons
	 * 
	 * @param modele
	 * @param taille
	 * @param controleur
	 */
	public PanelVignetteOptions(Modele modele, Dimension taille, Controleur controleur){
		this.modele = modele;
		this.controleur = controleur;
		this.img = modele.getImage();
		this.taille = taille;
		this.setBackground(Color.GRAY);
		initButtons();
	}
	
	/**
	 * Methode mettant a jour l'image du modele
	 */
	public void update(Observable arg0, Object arg1) {
		this.img = this.modele.getImage();
		this.getParent().repaint();
	}
	
	/**
	 * Tracage de l'image.
	 */
	public void paintComponent(Graphics g){
		g.drawImage(this.img.getBufferedImage(), 0, 0, this.taille.width, this.taille.height, this);
	}
	
	/**
	 * Methode d'initialisation et de personnalisation des boutons. 
	 */
	private void initButtons() {
		groupe = new ButtonGroup();
		JPanel buttonPan = new JPanel();
		zoomInIcon = new ImageIcon("plus.png");
		zoomIn = new JButton("zoomIn");
		zoomIn.setPreferredSize(DIM_BOUTTONS);
		zoomIn.setIcon(zoomInIcon);
		zoomIn.setBackground(Color.GRAY);
		zoomIn.addActionListener(new ButtonListener());
		buttonPan.add(zoomIn, BoxLayout.X_AXIS);
		groupe.add(zoomIn);
		
		zoomOutIcon = new ImageIcon("moins.png");
		zoomOut = new JButton("zoomOut");
		zoomOut.setBackground(Color.gray);
		zoomOut.setPreferredSize(DIM_BOUTTONS);
		zoomOut.setIcon(zoomOutIcon);
		zoomOut.addActionListener(new ButtonListener());
		buttonPan.add(zoomOut, BoxLayout.X_AXIS);
		groupe.add(zoomOut);
		
		undoIcon = new ImageIcon("undo.png");
		undo = new JButton("<-");
		undo.setBackground(Color.gray);
		undo.setPreferredSize(DIM_BOUTTONS);
		undo.setIcon(undoIcon);
		undo.addActionListener(new ButtonListener());
		buttonPan.add(undo, BoxLayout.X_AXIS);
		groupe.add(undo);
		
		redoIcon = new ImageIcon("redo.png");
		redo = new JButton();
		redo.setPreferredSize(DIM_BOUTTONS);
		redo.setIcon(redoIcon);
		redo.addActionListener(new ButtonListener());
		buttonPan.add(redo, BoxLayout.X_AXIS);
		groupe.add(redo);
		this.add(buttonPan);
	}
	
	/**
	 * Classe privee implementant ActionListener, servant a determiner les actions
	 * consequentes selons les boutons actives. 
	 * 
	 * @author Simon
	 */
	private class ButtonListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == zoomIn) {
				controleur.zoom(-1);
			}
			else if (e.getSource() == zoomOut) {
				controleur.zoom(1);
			}
			else if (e.getSource() == undo) {
				controleur.defaireCommande();
			}
			else if (e.getSource() == redo) {
				controleur.refaireCommande();
			}
		}
	}
	
}



